<?php
include_once    '../entity/folder_entity.php';
include         '../data/folder_dal.php';

class FolderMng
{
    function update($folderEntity)
    {
        $folderDAL = new $FolderDAL();
        if (!($this->validate($folderEntity->id, $folderEntity->name))) {
         return $folderDAL->update($folderEntity->id, $folderEntity->name, $folderEntity->userID, $folderEntity->url, $folderEntity->inTime, $folderEntity->inUser);
        } else {
            return -2;
        }
    }

    function get($id)
    {
        $where = "id = " .$id;
        $folderDAL = new FolderDAL();
        if ($result = $folderDAL->get($id)) {
            $row = $result->fetch_assoc();
            $folderEntity = new folderEntity();
            foreach ($row as $key => $value) {
                $folderEntity->$key = $value;
            }
        }
    }

    function getAll($start, $pageSize, $sortField, $sortDirection)
    {
        $orderBy = "ORDER BY " . $sortField . " " . $sortDirection . " LIMIT " . $start . ", " . ($pageSize * ($start + 1));
        $folderDAL = new FolderDAL();
        if ($result = $folderDAL->getAll($orderBy)) {
            while ($row = $result->fetch_assoc()) {
                foreach ($row as $key => $value) {
                $folderEntity->$key = $value;
                }
            $folderList[] = $folderEntity;
            }
        return $folderList;
        }
    }

    function delete($id)
    {
        $folderDAL = new FolderDAL();
        return $folderDAL->delete($id);
    }

    function validate($id, $url)
    {
        $folderDAL = new FolderDAL();
        return $folderDAL->validate($id, $name);
    }
}
?>
